#include <iostream>
#include <node.h>
#include <process.h>

extern "C" void launchCudaProgram(int *a, int *b, int *c, int n);

void LaunchCudaProgram(const v8::FunctionCallbackInfo<v8::Value> &args) {
    v8::Isolate *isolate = args.GetIsolate();
    v8::Local<v8::ArrayBuffer> arrayBufferA = args[0].As<v8::ArrayBuffer>();
    v8::Local<v8::ArrayBuffer> arrayBufferB = args[1].As<v8::ArrayBuffer>();
    v8::Local<v8::ArrayBuffer> arrayBufferC = args[2].As<v8::ArrayBuffer>();

    int *a = static_cast<int*>(arrayBufferA->GetContents().Data());
    int *b = static_cast<int*>(arrayBufferB->GetContents().Data());
    int *c = static_cast<int*>(arrayBufferC->GetContents().Data());
    int n = arrayBufferA->ByteLength() / sizeof(int);

    // 创建一个新进程并调用 CUDA 程序
    _beginthreadex(NULL, 0, [](void* param) {
        launchCudaProgram(static_cast<int*>(param), NULL, NULL, 10);
        return 0;
    }, static_cast<void*>(a), 0, NULL);

    args.GetReturnValue().Set(v8::Undefined(isolate));
}

void Init(v8::Local<v8::Object> exports) {
    NODE_SET_METHOD(exports, "launchCudaProgram", LaunchCudaProgram);
}

NODE_MODULE(addon, Init)
